/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: client.c
> Author:cjy
> Mail: 
> Created Time: 2024年08月13日 星期2 16时18分54秒
***************************************************/
// 开发一个计算服务器，能够计算两个整数的加减乘除
//          ##add#23#12##

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc,const char* argv[])
{
	int sock = 0;
	int ret = 0;
	char buf[1024] = {0};
	char dest[1024] = {0};
	int i = 0;
	int num1 = 0;
	int num2 = 0;
	int number = 0;
	float number1 = 0;
	// 初始化结构体变量用于保存服务器地址和信息
	struct sockaddr_in serAddr;
	memset(&serAddr,0,sizeof(serAddr));

	// 创建UDP套接字 UDP标识  传输模式(报文传输)
	sock = socket(AF_INET,SOCK_DGRAM,0);
	if(sock == -1) {
		perror("socket");
		exit(-1);
	}

	// 设置地址族
	serAddr.sin_family = AF_INET;
	// 设置端口号
	serAddr.sin_port = htons(6666);
	// 设置ip
	serAddr.sin_addr.s_addr = inet_addr("192.168.198.128");

	loop1:
	printf("1------- 加\n");
	printf("2------- 减\n");
	printf("3------- 乘\n");
	printf("4------- 除\n");
	printf("0------- 退出\n");
	printf("请输入你想要选择的操作\n");
	scanf("%d",&i);
	
	switch(i){
		case 1:
			printf("请输入数字1:\n");
			scanf("%d",&num1);
			printf("请输入数字1:\n");
			scanf("%d",&num2);
			sprintf(buf,"$$add$%d$%d$$",num1,num2);
			printf("%s\n",buf);

			// 发送数据
		ret = sendto(sock,buf,strlen(buf),0,(struct sockaddr *)&serAddr,sizeof(struct sockaddr));
		if(ret == -1) {
			perror("sendto");
		}
		memset(&buf,0,sizeof(buf));

		ret = recvfrom(sock,&number,sizeof(number),0,NULL,NULL);
		if(ret == -1){
			perror("recvfrom");
			exit(-1);
		}
		printf("%d\n",number);
		goto loop1;




			
		case 2:
			printf("请输入数字1:\n");
			scanf("%d",&num1);
			printf("请输入数字1:\n");
			scanf("%d",&num2);
			sprintf(buf,"$$jian$%d$%d$$",num1,num2);
			printf("%s\n",buf);

			// 发送数据
		ret = sendto(sock,buf,strlen(buf),0,(struct sockaddr *)&serAddr,sizeof(struct sockaddr));
		if(ret == -1) {
			perror("sendto");
		}
		memset(&buf,0,sizeof(buf));

		ret = recvfrom(sock,&number,sizeof(number),0,NULL,NULL);
		if(ret == -1){
			perror("recvfrom");
			exit(-1);
		}
		printf("%d\n",number);
		goto loop1;

	
		case 3:
			printf("请输入数字1:\n");
			scanf("%d",&num1);
			printf("请输入数字1:\n");
			scanf("%d",&num2);
			sprintf(buf,"$$cheng$%d$%d$$",num1,num2);
			printf("%s\n",buf);

			// 发送数据
		ret = sendto(sock,buf,strlen(buf),0,(struct sockaddr *)&serAddr,sizeof(struct sockaddr));
		if(ret == -1) {
			perror("sendto");
		}
		memset(&buf,0,sizeof(buf));

		ret = recvfrom(sock,&number,sizeof(number),0,NULL,NULL);
		if(ret == -1){
			perror("recvfrom");
			exit(-1);
		}
		printf("%d\n",number);
		goto loop1;
		case 4:
			
			printf("请输入数字1:\n");
			scanf("%d",&num1);
			printf("请输入数字1:\n");
			scanf("%d",&num2);
			sprintf(buf,"$$chu$%d$%d$$",num1,num2);
			printf("%s\n",buf);

			// 发送数据
		ret = sendto(sock,buf,strlen(buf),0,(struct sockaddr *)&serAddr,sizeof(struct sockaddr));
		if(ret == -1) {
			perror("sendto");
		}
		memset(&buf,0,sizeof(buf));

		ret = recvfrom(sock,&number1,sizeof(number1),0,NULL,NULL);
		if(ret == -1){
			perror("recvfrom");
			exit(-1);
		}
		printf("%.2f\n",number1);
		goto loop1;

	case 0:
		strcpy(buf,"exit");
		ret = sendto(sock,buf,strlen(buf),0,(struct sockaddr *)&serAddr,sizeof(struct sockaddr));
}
	//关闭套接字 
	close(sock);
    return 0;
}

